package com.guanghua.brick.db;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.sql.DataSource;

import com.guanghua.brick.BrickConstant;
import com.guanghua.brick.util.IConfig;

public class URLSQLConnection implements ISQLConnection {

	public Connection createConnection(String connId) throws Exception {
		IConfig config = BrickConstant.APP_DB_CONN_CONFIG;
		IConfig[] cs = config.getChildren("conn-cfg");
		
		//如果conn null表示取默认的，也就是第一个配置
		if (connId == null) {
			config = cs[0];
		//否则根据conn字符串匹配，如果没有取第一个
		} else {	
			config = cs[0];
			for (int i = 0; i < cs.length; i ++) {
				if (connId.equals(cs[i].getAttribute("id"))) {
					config = cs[i];
					break;
				}
			}
		}
		
		String driver = config.getText("driver");
		String url = config.getText("url");
		String user = config.getText("user");
		String password = config.getText("password");
		
		Class.forName(driver);
		return DriverManager.getConnection(url, user, password);
	}

	public DataSource createDataSource(String connId) throws Exception {
		throw new Exception("not support create datasource in url sql connection create impler");
	}

}
